﻿Imports System.Data
Imports Telerik.Web.UI
Imports System.Data.SqlClient

Partial Class ScreenPermissions
    Inherits System.Web.UI.Page

    Dim Users As New ScreenPermissionsSP

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")
        End If
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub OnSelectedIndexChangedHandler(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        'RadTreeView1.AppendDataBoundItems = True
        RadTreeView1.DataBind()
    End Sub

    Protected Sub RadComboBox1_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
        'Dim sql As String = "SELECT UserTable.id, isnull(Employee.emp_Num,0) as [emp_Num], isnull(Employee.FName,'-') as [FName], isnull(UserTable.UserName,'-') as [UserName] FROM [Employee] right outer join [UserTable] ON UserTable.emp_Num = Employee.emp_Num where (Employee.emp_Num like @emp_Num + '%')  or (Employee.FName like @FName + '%' ) or (UserTable.UserName like @UserName + '%')"

        odsUserList.SelectParameters("Operation").DefaultValue = "RtvUsersBySearch"
        odsUserList.SelectParameters("emp_Num").DefaultValue = e.Text
        odsUserList.SelectParameters("UserName").DefaultValue = e.Text
        odsUserList.SelectParameters("FName").DefaultValue = e.Text
        'sdsUsers.SelectParameters.Add("emp_Num", e.Text)
        'sdsUsers.SelectParameters.Add("UserName", e.Text)
        rcbUsers.DataBind()
    End Sub

    Protected Sub RadComboBox1_ItemDataBound(ByVal sender As Object, ByVal e As RadComboBoxItemEventArgs)
        'set the Text and Value property of every item
        'here you can set any other properties like Enabled, ToolTip, Visible, etc.
        e.Item.Text = (DirectCast(e.Item.DataItem, DataRowView))("UserName").ToString()
        e.Item.Value = (DirectCast(e.Item.DataItem, DataRowView))("id").ToString()
    End Sub

    Protected Sub RadTreeView1_NodeDataBound(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)
        e.Node.Value.ToString()
        lblIsPermitted.Visible = False

        If Not rcbUsers.SelectedValue = "" Or rcbUsers.SelectedValue IsNot Nothing Then
            Dim DSPrePermissions As New DataSet
            DSPrePermissions = PreDefinedPermissions()

            If DSPrePermissions.Tables(0).Rows.Count >= 1 Then

                lblIsPermitted.Visible = False

                Dim x As Integer = 0
                While x < DSPrePermissions.Tables(0).Rows.Count
                    If e.Node.Value.ToString() = DSPrePermissions.Tables(0).Rows(x).Item("MenuID").ToString() Then
                        e.Node.Checked = True
                    End If
                    System.Math.Max(System.Threading.Interlocked.Increment(x), x - 1)
                End While
            Else
                If rcbUsers.SelectedValue <> "" Then
                    lblIsPermitted.Visible = True
                End If

            End If
        End If
    End Sub

    Public Shared Function GetPermission(ByVal Userid As String) As Boolean

        Dim str As New DBConnectionClass

        Using conn As New SqlConnection(str.ConnectionString)
            conn.Open()
            Dim cmd As New SqlCommand("select * from ScreenPermissions where UserID = '" + Userid + "'", conn)
            Dim reader As SqlDataReader
            reader = cmd.ExecuteReader()
            If (reader.Read()) Then
                Return True
            Else
                Return False
            End If
            conn.Close()
        End Using
    End Function


    Public Function PreDefinedPermissions() As DataSet

        Dim selectedDatatset As New DataSet
        selectedDatatset = Users.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "rtvPreDefinedPermissions", , rcbUsers.SelectedValue)

        Return selectedDatatset

    End Function


    Protected Sub ibtnSave_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnSave.Click
        Dim UserID As String = rcbUsers.SelectedValue
        Dim IDs As IList(Of RadTreeNode) = RadTreeView1.CheckedNodes

        Users.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeletePermission", , UserID)

        Dim i As Integer = 0
        While i < IDs.Count
            Dim node As RadTreeNode = DirectCast(IDs(i), RadTreeNode)
            'Dim parentId As Integer = IIf(node.ParentNode Is Nothing, -1, IDs.IndexOf(node.ParentNode))
            Dim MenuID As String = node.Value.ToString()


            Users.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "SavePermission", MenuID, UserID)

            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While

        Response.Redirect("~/Default.aspx")

    End Sub

End Class
